﻿#region File Description
//-----------------------------------------------------------------------------
// Javier Cantón Ferrero
// javiercantonferrero@gmail.com
// @jcant0n
// http://geeks.ms/blogs/jcanton/
// http://xnacommunity.codeplex.com
//
// MVP XNA/DirectX
//-----------------------------------------------------------------------------
#endregion


#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Threading;
using Microsoft.Xna.Framework.Input;
using MyGame.Screens.Transitions;
#endregion

namespace MyGame.Screens
{
    public class LoadingScreen : Screen
    {
        //Fields
        Thread thread;
        TimeSpan time;
        TimeSpan timeLoading = TimeSpan.FromMilliseconds(500);
        int i = 0;
        Vector2 position = new Vector2(260, 750);
        String text = "Loading";
        String[] strs = { "Loading", "Loading.", "Loading..", "Loading..." };

        Texture2D logo;
        Texture2D toro;

        #region Properties

        #endregion

        #region Initialize
        public LoadingScreen() : base()
        {
            
        }

        #endregion

        #region Public Methods
        public override void LoadContent()
        {
            logo = GameGlobals.content.Load<Texture2D>("logo");
            toro = GameGlobals.content.Load<Texture2D>("torito");
        }

        public override void Initialize()
        {
            time = TimeSpan.FromSeconds(2);
            thread = new Thread(new ThreadStart(InitializeScreenManager));
            thread.Start();
        }

        public override void Update()
        {
            time -= GameGlobals.gameTime.ElapsedGameTime;
            timeLoading -= GameGlobals.gameTime.ElapsedGameTime;

            if (time < TimeSpan.Zero && thread.ThreadState == ThreadState.Stopped)
                ScreenManager.TransitionTo("MainMenu", "BlackFade");

            if (timeLoading < TimeSpan.Zero )
            {
                timeLoading = TimeSpan.FromMilliseconds(200);
                i++;
            }

            text = strs[i%4];
        }

        public override void Draw()
        {
            spriteBatch.Begin();
            GameGlobals.DrawCenterTexture(logo, 40, spriteBatch);
            GameGlobals.DrawCenterTexture(toro, 130, spriteBatch);
            spriteBatch.DrawString(GameGlobals.defaultFont, text, position, Color.Black);
            spriteBatch.End();
        }

        #endregion

        #region Private Methods
        private void InitializeScreenManager()
        {
            //Add all screens here
            ScreenManager.AddScreen("BlackFade", new Fade(Color.Black));
            ScreenManager.AddScreen("MainMenu", new MainMenuScreen());
            ScreenManager.AddScreen("PlayGame", new GameScreen());
            //...
        }

        #endregion
    }
}
